/**
 * Created by DukeCrushIt on 2015/8/21.
 */
class DragonLoader {
    public constructor(){
        this.dragonBonesFac = new dragonBones.EgretFactory();
    }
    public dragonBonesFac:dragonBones.EgretFactory;
    public overCallObj:any;
    public overCall:Function;
    public overCallParam:any;
    public loadingName:string;
    private loading:string[] = [];
    public loadgroup(name:string,completeObj:any=null, completeFunc:Function=null,param:any=null){
        if( this.loading.indexOf(name) == -1){
            this.overCall = completeFunc;
            this.overCallObj = completeObj;
            this.overCallParam = param;
            this.loadingName = name;
            this.loading.push(name);
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.loadGroup(name);
        }
    }

    private onResourceLoadComplete(event:RES.ResourceEvent):void {

        if( this.loading.indexOf(event.groupName) != -1) {
            this.dragonBonesFac.addDragonBonesData(dragonBones.DataParser.parseDragonBonesData(RES.getRes(event.groupName + "_json")));
            this.dragonBonesFac.addTextureAtlas(new dragonBones.EgretTextureAtlas(RES.getRes(event.groupName + "_texture_png"), RES.getRes(event.groupName + "_texture_json")));
        }

        if( event.groupName == this.loadingName){
            if( this.overCall != null)
                this.overCall.call(this.overCallObj,this.overCallParam);
            this.overCall = null;
            this.overCallObj = null;
            this.overCallParam = null;
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        }
    }

    private static _instance:DragonLoader;
    public static getInstance():DragonLoader{
        if( DragonLoader._instance == null )
            DragonLoader._instance = new DragonLoader();
        return DragonLoader._instance;
    }
}